package com.yanglanting.disk.file.control;

import com.alibaba.fastjson.JSON;
import com.yanglanting.disk.file.entity.SearchFileEntity;
import com.yanglanting.disk.file.service.FileIoService;
import com.yanglanting.disk.file.service.FileService;
import com.yanglanting.disk.mybatis.entity.PtFileEntity;
import com.yanglanting.disk.mybatis.entity.PtRoleEntity;
import com.yanglanting.disk.mybatis.entity.PtUserEntity;
import com.yanglanting.disk.mybatis.flexigrid.PagingEntity;
import com.yanglanting.disk.mybatis.flexigrid.TblResponseEntity;
import com.yanglanting.disk.spring.control.BaseController;
import com.yanglanting.disk.user.entity.SearchUserEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/file")
public class FileController extends BaseController {
    @Resource
    private FileService fileService;

    @Resource
    private FileIoService fileIoService;



    @RequestMapping("/fileAll")
    public String fileAll(){
        return "file/filePage";
    }
    @RequestMapping("/syncFileData")
    @ResponseBody
    public String syncFileData(){
        fileIoService.syncFileData();
        return successJson("file.sync.data.success");
    }


    //分页查询
    @RequestMapping("/queryFileInfoListByPage")
    @ResponseBody
    public String queryFileInfoListByPage(SearchFileEntity entity, PagingEntity paging){
        TblResponseEntity<PtFileEntity> tblResponseEntity = fileService.queryFileInfoListByPage(entity,paging);
        return JSON.toJSONString(tblResponseEntity);
    }


    @RequestMapping("/queryFileInfoById")
    @ResponseBody
    public String queryFileInfoById(@RequestParam("fileUuid") String fileUuid){
       PtFileEntity ptFileEntity = fileService.queryFileInfoById(fileUuid);
       return JSON.toJSONString(ptFileEntity);
    }

    @RequestMapping("/mkdirFileInfo")
    @ResponseBody
    public String mkdirFileInfo(@RequestParam("parentId") String parentId,@RequestParam("folderName") String folderName){

        fileIoService.mkdirFileInfo(parentId,folderName);
        return successJson("file.mkdir.success");
    }


    @RequestMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(MultipartHttpServletRequest request,@RequestParam("parentId") String parentId) throws IOException {
        MultipartFile multipartFile = request.getFile("fileName");
        String fileName = multipartFile.getOriginalFilename();

        fileIoService.uploadFile(parentId,fileName,multipartFile.getInputStream());

        return successJson("file.upload.success");
    }


    @RequestMapping("/copyFile")
    @ResponseBody
    public String copyFile(@RequestParam("parentId") String parentId,@RequestParam("srcUuidList") String srcUuidList) {
        fileIoService.copyFile(parentId,srcUuidList);
        return successJson("file.copy.success");
    }

/**yang**/
    @RequestMapping("/modifyFileInfoById")
    @ResponseBody
    public String  modifyFileInfoById(PtFileEntity entity){
        fileService.modifyFileInfoById(entity);
        return successJson("modify.file.success");
    }
/**yang**/

     @RequestMapping("/downFile/{fileUuid}")
     @ResponseBody
     public ResponseEntity<byte[]> downFile(@PathVariable("fileUuid") String fileUuid){
     ResponseEntity<byte[]> responseEntity = fileIoService.downFile(fileUuid);
     return responseEntity;
}
    @RequestMapping("/getShareUrl")
    @ResponseBody
   public String getShareUrl(@RequestParam("fileUuid") String fileUuid){
         String url  = fileIoService.getShareUrl(fileUuid);
         return successJson("file.share.success",url);

   }
    /**
     * 将文件移至回收站
     * @param fileUuid 文件ID
     * @return 操作结果
     */
    @RequestMapping("/deleteToRecycle")
    @ResponseBody
    public String deleteFileToRecycle(@RequestParam("fileUuid") String fileUuid) {
        fileService.deleteFileToRecycle(fileUuid);
        return successJson("file.delete.to.recycle.success");
    }

    @RequestMapping("/queryShareUrl")
    @ResponseBody
    public String queryShareUrl(@RequestParam("fileUuid") String fileUuid,@RequestParam("fileValidity") Integer fileValidity){
        String url = fileIoService.queryShareUrl(fileUuid,fileValidity);
        return successJson("file.share.success",url);
    }

}
